package nw_client_versions;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Transaction;

public class NWClientVersions {

	static Jedis jd;
	static Transaction tx;
	static int cntNew=0;
	static int cntOld = 0;

	 //List of Keys
	
	static String keyPrefix = "prod:backup:nw";
	static String keyVersion = "versions";
	static String keyLicApp = "licApp";
	static String keyClients = "clients";
	
	
	static Hashtable<String,Integer> clientList = new Hashtable<String,Integer>();

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		String inputFile = args[0];
		String unit = args[1];
		BufferedReader br = new BufferedReader(new FileReader(inputFile));
		String line;


		String keyTotalVersions = keyPrefix + ":" + unit + ":" + keyVersion;
		String keyTotalLicApp = keyPrefix + ":" + unit + ":" + keyLicApp;
		String keyTotalClients = keyPrefix + ":" + unit + ":" + keyClients;
		String keyTotalClientEnabled = keyTotalClients + ":enabled";
		String keyVerionsKeyList = keyPrefix + ":" + unit + ":keys";
		String keyLicAppKeyList = keyPrefix + ":" + unit + ":keys";

		int cnt = 0;
		String client = "";
		String licApp = "";
		String NWVersion = "";
		String redisHost = "172.18.38.14";
		jd = new Jedis(redisHost);

		Jedis jdpl = new Jedis(redisHost);
		tx = jdpl.multi();

		
		tx.rename(keyTotalVersions, keyTotalVersions + ":old");
		
		tx.del(keyTotalVersions);
		tx.del(keyTotalLicApp);
		tx.del(keyTotalClients);
		tx.del(keyTotalClientEnabled);

		Set<String> versionList  = jd.smembers(keyVerionsKeyList);

		for (String verKey : versionList ) {
			tx.del(verKey);
		}

		Set<String> licAppList  = jd.smembers(keyLicAppKeyList);

		for (String verKey : licAppList ) {
			tx.del(verKey);
		}

		tx.del(keyVerionsKeyList);
		tx.del(keyLicAppKeyList);

		//tx.exec();

		//System.exit(0);

		Boolean getNextLine = false;
		String lastKey ="";
		//Jedis jdpl = new Jedis(redisHost);
		//tx = jdpl.multi();

		line = br.readLine();
		try {
			while (line != null) {
				String val;
				String key;

				if (line.contains("Current query set")) {line = br.readLine(); continue;}


				if (line.trim().length() > 0) {
					if (line.trim().startsWith("name")) {
						key = line.trim().split(":")[0];
						val = line.trim().split(":")[1];
						val = val.replace(";", "");
						client = val.trim();
						lastKey = key;
					}
					if (line.trim().startsWith("NetWorker version")) {
						key = line.trim().split(":")[0];
						val = line.trim().split(":")[1];
						val = val.replace(";", "");
						NWVersion = val.trim();
						lastKey = key;
					}
					if (line.trim().startsWith("licensed applications")) {
						key = line.trim().split(":")[0];
						val = line.trim().split(":")[1];
						val = val.replace(";", "");
						if (val.endsWith(",")) {getNextLine = true;}
						val = val.replace(",", "");
						//val = val.replace("/\"", "");
						
						val = val.replace("/", " ");
						//val = val.replace(":\\\"", "");
						val = val.replace("\"N", "N");
						val = val.replace("1\"", "1");
						licApp = val.trim();
						lastKey = key;
						//System.out.println(val);
						//System.exit(0);
					}






					boolean execredis = false; 

					/**
					if (NWVersion.length() > 0) {
						if (checkClient(clientList,client) == false) {
							tx.sadd("nw:clients:enabled", client);
							tx.zincrby("nw:versions", 1, NWVersion);
						}

					/**	
						if (jd.sismember("nw:clients:enabled", client) == false) {
							jd.sadd("nw:clients:enabled", client);
							tx.zincrby("nw:versions", 1, NWVersion);
						}
					 **/	





					if (licApp.length() > 0) {
						tx.zincrby(keyTotalLicApp, 1, licApp);
					}



					//tx.zincrby("nw:clients", 1, client);
					//tx.sadd("nw:clients:enabled", client);

					if (lastKey.contains("NetWorker version")) {
						tx.sadd(keyVerionsKeyList, keyTotalVersions + ":" + NWVersion);
						tx.sadd(keyTotalVersions + ":" + NWVersion, client);
						if (checkClient(clientList,client) == false) {
							tx.sadd(keyTotalClientEnabled, client);
							tx.zincrby(keyTotalVersions, 1, NWVersion);
						}
					}

					if (lastKey.contains("licensed applications")) {
						if (licApp.length() > 0) {
							tx.sadd(keyLicAppKeyList, keyTotalLicApp + ":" + licApp);
							tx.sadd(keyTotalLicApp + ":" + licApp, client);
						}
					}

					//pipeline.sync();
					//tx.exec();
					//pipeline.discard();



					/**    		

	        		String key = line.trim().split(":")[0];
	        		String val = line.trim().split(":")[1];
	        		if (key.contains("name")) {
	        			client = val;
	        		} else if (key.contains("NetWroker version")) {
	        			NWVersion = val;
	        			jd.zincrby("nw:versions", 1, NWVersion);
	        		} else if (key.contains("licensed applications")) {
	        			licApp = val;
	        			jd.zincrby("nw:licApp", 1, licApp);
	        		}

					 **/  		
					System.out.println(line.trim());

					while (getNextLine) {
						cnt++;
						line = br.readLine();
						line = line.trim();
						if (line.endsWith(",")) {getNextLine = true;} else {getNextLine = false;}
						val = line.replace(";", "");
						val = val.replace(",", "");


						if (lastKey.contains("NetWorker version")) {
							tx.sadd(keyVerionsKeyList, keyTotalVersions + ":" + val);
							tx.sadd(keyTotalVersions + ":" + val, client);
						}

						if (lastKey.contains("licensed applications")) {
							tx.sadd(keyVerionsKeyList, keyTotalLicApp +":" + val);
							tx.sadd(keyTotalLicApp + ":" + val, client);
							//System.out.println(client);
							//System.out.println(lastKey);
							//System.out.println(line.trim());

						}
						//System.out.println(cnt);
					}


				}
				line = br.readLine();
			}


		} finally {
			br.close();
			tx.exec();

			//System.out.println(cntOld);
			//System.out.println(cntNew);

		}
	}



	public static boolean checkClient(Hashtable<String,Integer> myMap,String type) {

		int t = 0;
		boolean bt = false;

		if (myMap.containsKey(type)) {
			t = myMap.get(type) +1;
			cntOld++;
			//System.out.println("Found:" + type);
			bt = true;
		} else {
			t =1;
			bt =false;
			//System.out.println("New:" + type);
			cntNew++;
		}

		myMap.put(type, t);

		return bt;
	}
}
